package web;

import pojo.User;
import service.userService.userService;
import service.userServiceImpl.userServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
    userService userservice=new userServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     String username=req.getParameter("username");
     String password=req.getParameter("password");
     String errorUsername="";
     String errorPassword="";
        User user = userservice.login(username,password);
        if (user!=null){
            //获取是否勾选记住
            String r=req.getParameter("remember");
            if ("1".equals(r)){
                //如果勾选创建COOKIE会话
                Cookie c_username=new Cookie("username",username);
                Cookie c_password=new Cookie("password",password);
                //设置存储的时间
                c_username.setMaxAge(7*24*60*60);//7天
                c_password.setMaxAge(7*24*60*60);
                //将cookie发送到客户端
                resp.addCookie(c_username);
                resp.addCookie(c_password);
            }else{
                Cookie[] cookies= req.getCookies();
                for (Cookie cookie:cookies){
                    if (cookie.getName().equals("username")){
                        System.out.println(cookie.getName().equals("username"));
                        System.out.println(cookie.getName());
                        System.out.println(username);
                        Cookie newcookie=new Cookie("username",errorUsername);
                        //删除cookie需要创建一个跟要删除的cookie同name的cookie然后设置maxage为0，并resp后覆盖要删除的cookie对象
                        newcookie.setMaxAge(0); //立即删除型
                        resp.addCookie(newcookie); //重新写入，将覆盖之前的
                        System.out.println("这波删了吗");
                    }else if (cookie.getName().equals("password")){
                        Cookie newcookie=new Cookie("password",errorPassword);
                        newcookie.setMaxAge(0); //立即删除型
                        resp.addCookie(newcookie); //重新写入，将覆盖之前的
                    }
                }
            }
            //登陆成功！！
            //创建session会话
            HttpSession session=req.getSession();
            session.setAttribute("user",user);
            String contextPath = req.getContextPath();
            //跳转到selectAllServlet页面
            resp.sendRedirect(contextPath+"/brand.html");
        }else {
            //返回到登陆页面，并且在顶部传入req数据域中的提示
          req.setAttribute("mis","用户名或密码不符合阿怒的要求");
          req.getRequestDispatcher("/login.jsp").forward(req,resp);
          req.removeAttribute("mis");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
